package main
import (
	"fmt"
	"net"
	"time"
)
var ch chan byte
func main(){
	
	defer time.Sleep(time.Second * 3)	//延时3秒
	
	/*第一步：接入127.0.0.1:8000端口*/
	conn, err := net.Dial("tcp", "127.0.0.1:8000")
	if err != nil{
		fmt.Println("Dial err : ", err)
		return
	}
	defer conn.Close()
	go jieshou(conn)
	time.Sleep(time.Second)
	for{
		input(conn)
	}
}

func input(conn net.Conn){
	var str string
	fmt.Scan(&str)
	_, err1 := conn.Write([]byte(str))
	if err1 != nil{
		fmt.Println("发送失败，错误 ：", err1)
		return
	}
}

func jieshou(conn net.Conn){
	addr := conn.RemoteAddr()
	/*第四步，读取服务器端信息*/
	buf := make([]byte, 4096)
	for{
		sum, err := conn.Read(buf)
		if err != nil{
			fmt.Println("Read err : ", err)
			return
		}
		/*输出服务器端消息*/
		fmt.Println(addr, " ：", string(buf[:sum]))
	}
}